//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.DicAdmAreaCode;
import com.aidong.model.wills.DicShopCode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class DicAdmAreaCodeDao extends BaseDao<DicAdmAreaCode, String> {
	public DicAdmAreaCodeDao() {
	}

	public List<DicAdmAreaCode> getAllAreaList() {
		return this.queryList("(beginTime is null or beginTime<getdate()) and (endTime is null or endTime>getdate())", (Object[])null, (new OrderSet()).asc("areaType").asc("code"), -1);
	}

	public List<DicAdmAreaCode> getAreaListByShop(String shopCode) {
		String sql = "select c.* from DicAdmAreaCode c\ninner join DicAdmAreaList l on c.code=l.areaCode\nwhere shopCode = :p1 and (beginTime is null or beginTime<getdate()) and (endTime is null or endTime>getdate()) order by c.code";
		return this.queryListBySql(sql, new Object[]{shopCode});
	}

	public List<DicShopCode> getAreaShopList(String areaCode, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join DicAdmAreaList a on s.code=a.shopCode and areaCode = :p1\nwhere status>0 order by s.code";
		} else {
			sql = "select s.code, s.chName, status from DicShopCode s\ninner join DicAdmAreaList a on s.code=a.shopCode\nwhere areaCode = :p1 and status>0 order by s.code";
		}

		return this.queryListBySql(DicShopCode.class, sql, new Object[]{areaCode});
	}

	public List<String[]> getAllAreaShopList() {
		ArrayList list = new ArrayList();
		String sql = "select areaCode, shopCode from DicAdmAreaList";
		SQLQuery query = this.getSession().createSQLQuery(sql);
		List ls = query.list();
		Iterator i$ = ls.iterator();

		while(i$.hasNext()) {
			Object[] o = (Object[])i$.next();
			list.add(new String[]{o[0].toString(), o[1].toString()});
		}

		return list;
	}

	public void updateAreaShop(String areaCode, List<String> shopList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from DicAdmAreaList where AreaCode = :p1");
		query.setString("p1", areaCode);
		query.executeUpdate();
		query = session.createSQLQuery("insert into DicAdmAreaList (AreaCode, ShopCode) values (:p1, :p2)");
		query.setString("p1", areaCode);
		Iterator i$ = shopList.iterator();

		while(i$.hasNext()) {
			String shopCode = (String)i$.next();
			query.setString("p2", shopCode);
			query.executeUpdate();
		}

	}
}
